home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6676 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.5 KB  |  145 lines

  1. Newsgroups: comp.lang.c
  2. Path: news.sprintlink.net!news1!news
  3. From: rclark@iquest.net (Robert B. Clark)
  4. Subject: Re: Easiest way to center a string?
  5. X-Nntp-Posting-Host: ind-002-236-123.iquest.net
  6. Message-ID: <3124a220.3624775@news.iquest.net>
  7. Sender: news@iquest.net (News Admin)
  8. Organization: IQuest Internet, Inc.
  9. X-Newsreader: Forte Agent .99d/16.182
  10. References: <4fobka$1st@parlor.hiwaay.net> <3120d412.7714326@news.iquest.net> <4ftelp$b7l@sun001.spd.dsccc.com>
  11. Date: Fri, 16 Feb 1996 16:21:27 GMT
  12.  
  13. On 14 Feb 1996 19:54:33 GMT, jmccarty@spd.dsccc.com (Mike McCarty)
  14. wrote:
  15.  
  16. >In article <3120d412.7714326@news.iquest.net>,
  17. >Robert B. Clark <rclark@iquest.net> wrote:
  18. >)Compute the left column by subtracting half of the string length from
  19. >)half of the screen width; e.g.,
  20. >)
  21. >)    x=MAXCOL / 2 - strlen(str) / 2;
  22. >)
  23. >Please do =not= use this code as it stands. It may do terrible things to
  24. >your screen if the string does not fit into the field. Always check for
  25. >fits before using such techniques.
  26.  
  27. Of course, bounds checking is a good idea.  But since this was a
  28. homework assignment, I stuck to the essentials. :-)
  29.  
  30. Here is something that may be a little more robust.  This is wriiten for
  31. DOS and uses a few DOS-specific functions and structs:
  32.  
  33. /* Center.c -   Turbo C v3.0
  34.    Demonstrates how to center a string on a line for any size window.
  35.    Usage: CENTER [winleft] [wintop] [winright] [winbottom]
  36.    Written by Robert B. Clark <rclark@iquest.net>  13 Feb 1996 */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <conio.h>   /* For gotoxy(), cprintf(), window(), etc. */
  41. #include <string.h>  /* For strlen() */
  42.  
  43. #define WINWIDTH (ti.winright-ti.winleft+1)
  44.  
  45. struct text_info ti;   /* Used in various functions */
  46.  
  47. /* Centers a string in a window.
  48.    Uses global text_info struct ti.
  49.    Returns -1 if computed left column is out of bounds. */
  50. int Center(const char *str)
  51. {
  52.    int lc, len;
  53.  
  54.    len=strlen(str);
  55.    gettextinfo(&ti);                   /* Get text-mode video info */
  56.    lc=WINWIDTH / 2 - len / 2 + 1;      /* Compute left column */
  57.    if (len>WINWIDTH) {
  58.       fprintf(stderr,"\n\aWidth of text window is insufficient.");
  59.       lc=-1;
  60.    }
  61.    else {
  62.       gotoxy(lc,wherey());
  63. #ifdef WINDOWDRESSING               /* Just for fun */
  64.       len=ti.attribute;             /* Get current text attribute */
  65.       textcolor((len & 15)+BLINK);  /* Make text foreground blink */
  66.       cputs(str);
  67.       textcolor(len & 15);          /* Reset original attribs */
  68. #else
  69.       cputs(str);
  70. #endif
  71.    }
  72.    return lc;
  73. } /* Center */
  74.  
  75.  
  76. /* Simply prints current window x-coordinates (nominally 1..80)
  77.    Uses global text_info struct ti. */
  78. void PrintXCoords(void)
  79. {
  80.    int j; char c;
  81.  
  82.    cputs("\n\r");
  83. /* Print x-coordinates MSD (0,10,20,30...) */
  84.    j=1; c='1';
  85.    while(j<=WINWIDTH) {
  86.       if (j / 10 * 10 == j)
  87.          putch(c++);
  88.       else
  89.          putch(' ');
  90.       if (c>'9') c='0';
  91.       j++;
  92.    }
  93. /* Print x-coordinates LSD (1,2,3...) */
  94.    j=1; c='1';
  95.    while(j<=WINWIDTH) {
  96.       putch(c++);
  97.       if (c>'9') c='0';
  98.       j++;
  99.    }
  100. } /* PrintXCoords */
  101.  
  102.  
  103. /* Set up text window */
  104. void SetWindow(int x1, int y1, int x2, int y2, int fore, int back)
  105. {
  106.    textattr(WHITE+(BLACK<<4));   /* Reset screen to white on black */
  107.    clrscr();
  108.    window(x1,y1,x2,y2);          /* Create new text window */
  109.    textattr(fore+(back<<4));     /* Change window colors so it */
  110.    clrscr();                     /* will stand out */
  111. } /* SetWindow */
  112.  
  113.  
  114. int main(int argc, char *argv[])
  115. {
  116.    const char str[]="This text is centered";
  117.    int x,x1=10,y1=8,x2=70,y2=20;
  118.  
  119.    if (argc>1) {                 /* Get command line coordinate */
  120.       x1=atoi(argv[1]);          /* values if specified */
  121.       if (argc>2) y1=atoi(argv[2]);
  122.       if (argc>3) x2=atoi(argv[3]);
  123.       if (argc>4) y2=atoi(argv[4]);
  124.    }
  125.    SetWindow(x1,y1,x2,y2,WHITE,GREEN);
  126.    gettextinfo(&ti);       /* Needed for WINWIDTH */
  127.    cprintf("Requested window coordinates are (%d,%d) to (%d,%d)."
  128.           "\n\rCurrent window coordinates are   (%d,%d) to (%d,%d)."
  129.           "\n\rString length is %d."
  130.           "\n\rWindow width is %d.\n\n\r",
  131.           x1,y1,x2,y2,ti.winleft,ti.wintop,ti.winright,ti.winbottom,
  132.           strlen(str),WINWIDTH);
  133.    x=Center(str);
  134.    PrintXCoords();
  135.    if (x!=-1) cprintf("\n\rCentered text at %d.",x);
  136.    window(1,1,ti.screenwidth,ti.screenheight);  /* Restore window */
  137.    gotoxy(1,22);                                /* to full screen */
  138.    return x;
  139. }
  140.  
  141. --
  142. Robert B. Clark <rclark@iquest.net>
  143. "Be wary of strong spirits.  It can make you shoot at tax collectors...
  144. and miss." --RAH
  145.